<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="850px"
  >
    <div class="form-box">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
      <el-form-item label="店铺" prop="tenantId" v-hasPermi="['searchTenantID']">
        <el-select
          v-model="form.tenantId"
          placeholder="请选择店铺"
          clearable
          filterable
        >
          <el-option
            v-for="dict in tenantList"
            :key="dict.tenantId"
            :label="dict.tenantName"
            :value="dict.tenantId"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="APP类型" prop="appType">
          <el-select v-model="form.appType" placeholder="请选择APP类型" >
            <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.name"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="版本号" prop="appVersionNum">
          <el-input-number :controls="false" style="width: 250px;" :precision="0" v-model="form.appVersionNum" placeholder="请输入版本号,数值" />
        </el-form-item>
        <el-form-item label="版本名称" prop="appVersionName">
          <el-input  v-model="form.appVersionName" placeholder="请输入版本名称" />
        </el-form-item>
        <el-form-item label="更新类型" prop="isUpdate">
          <el-select v-model="form.isUpdate" placeholder="请选择" >
            <el-option
              v-for="dict in isUpdateList"
              :key="dict.value"
              :label="dict.name"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="更新内容" prop="appUpdateRemark">
          <el-input style="width: 600px;" rows="5" type="textarea"  v-model="form.appUpdateRemark" placeholder="请输入" />
        </el-form-item>
        <el-row v-loading="loading">
          <el-form-item label="安装包" prop="" >
            <el-upload
                    action="#"
                    :file-list="imgShowList"
                    :show-file-list="true"
                    :limit="1"
                    :http-request="uploadApk"
                    :on-success="handleUploadSuccess"
                    :on-remove="handleRemove"
                  >
                  <el-button size="small" type="primary" v-if="imgShowList.length==0">点击上传</el-button>
                  </el-upload>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="下载链接" prop="appDownloadUrl" >
            <el-input style="width:400px" v-model="form.appDownloadUrl" placeholder="请输入下载链接或者上传安装包" />
          </el-form-item>
        </el-row>
        

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" >提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from '@/utils/auth'
import {spliceUploadHandle} from "@/api/spliceUpload.js"
import appApi from "@/api/B2Cmanage/appManage"
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      typeList: [
        {
          name: "安卓",
          value: "1"
        },
        // {
        //   name: "苹果",
        //   value: "2"
        // }
      ],
      isUpdateList: [
        {
          name: "弹窗确认",
          value: "solicit"
        },
        {
          name: "强制更新",
          value: "forcibly"
        }
      ],
      title: "新增",
      isEdit: false,
      imgShowList: [],
      // 表单参数
      form: {
        appType:'',
        appDownloadUrl:'',
        appVersionName:'',
        appUpdateRemark:'',
        appVersionNum:undefined,
        isUpdate:'',
        tenantId:''
      },
      rules: {
        tenantId:[
          { required: true, message: "请选择", trigger: "change" }
        ],
        appType: [
          { required: true, message: "请选择", trigger: "change" }
        ],
        appVersionName: [
          { required: true, message: "请输入", trigger: "change" }
        ],
        appVersionNum: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        appUpdateRemark:[
        { required: true, message: "请输入", trigger: "blur" }
        ],
        appDownloadUrl:[
          { required: true, message: "请输入下载链接或上传安装包", trigger: "blur" }
        ],
        isUpdate:[
        { required: true, message: "请选择", trigger: "change" }
        ]
      },
      headers:{

      }
    };
  },
  computed: {
    tenantList() {
      return this.$store.getters.tenantList
    }
  },
  created() {
    this.headers = {
      token:'Bearer ' + getToken() ,
      Authorization:'Bearer ' + getToken()
    }
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/app";
  },
  methods: {
    // 打开弹窗
    open(flag, data) {
      this.imgShowList = [];
      if (flag == "edit") {
        
      } else {
        this.isEdit = false;
      }
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.reset();
      });
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    async uploadApk(file){
      // console.log("%c Line:94 🍯 file", "color:#7f2b82", file);
      //上传到后台
      this.loading = true
      try {
        let uploadImg = await spliceUploadHandle(file.file)
        // console.log("%c Line:157 🥑 uploadImg", "color:#b03734", uploadImg);
        file.data = {
          filePath: uploadImg.filePath,
          fileName: uploadImg.fileName,
        };
        file.code = 200;
        this.loading = false
        return file;
      } catch {
        file.code = 500;
        this.loading = false
        return file;
      }
      
    },
    submitForm(formName) {
      this.$refs["" + formName].validate(valid => {
        if (valid) {
          
          if (this.isEdit) {
            // 编辑
            // appApi.editImage(this.form).then(res => {
            //   console.log(
            //     "%c 🍊 res: ",
            //     "font-size:20px;background-color: #3F7CFF;color:#fff;",
            //     res
            //   );
            //   this.$message.success(res.msg);
            //   this.dialogVisible = false;
            //   this.$parent.search();
            // });
          } else {
            // 新增
            appApi.add(this.form).then(res => {
             
              this.$message.success(res.msg);
              this.dialogVisible = false;
              this.$parent.getList();
            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },
    // APP上传成功
    handleUploadSuccess(res, file, fileList) {
      console.log("%c Line:205 🌭 res", "color:#6ec1c2", res);
      if (res.code == 200) {
        // 取得新上传的文件
        this.imgShowList = [{ url: res.data.filePath, name: res.data.fileName }];
          this.form.appDownloadUrl  = res.data.filePath
      }
    },
    // 删除APP
    handleRemove(response, fileList, file) {
      this.imgShowList = []
      this.form.appDownloadUrl = ''
    },
  
    // uploadNumber(file, fileList) {
    //   //限制图片数量
    //   this.$modal.msgError("最多只能上传1个");
    // },
    close() {
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}
.form-box {
  max-height: calc(100vh - 20vh - 100px);
  overflow-y: auto;
  overflow-x: hidden;
  padding-top: 20px;
}
.form-box >>> .el-input {
  width: 250px;
}

.form-box >>> img {
  width: 100%;
}
.form-box >>> .el-upload-dragger {
  width: 148px;
  height: 148px;
}

.form-box >>> .el-form-item__content {
  max-width: 655px;
}
.hiddenupload >>> .el-upload--picture-card {
  display: none;
}
</style>
